/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package drt;

import evaluation.Graph;
import java.util.LinkedHashSet;
import java.util.Map;
import logic.Type;

/**
 *
 * @author phong
 */
public class DrtVariable implements DrtExpression {
    
    Type type;
    String name;
    DrtVariable eqVar;

    public DrtVariable(String name, Type type) {
        this.name = name;
        this.type = type;
        eqVar = null;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public boolean setEqVar(DrtVariable var) {
        if (this.type.matches(var.type)) {
            this.eqVar = var;
            return true;
        }
        else 
            return false;
    }
    
    public DrtVariable getEqVar() {
        return this.eqVar;
    }
    
    @Override
    public Type getType() {
        return this.type;
    }
    
    @Override
    public void setType(Type t) {
        if (this.type.equals(Type.UNKNOWN_TYPE))
            this.type = t;
        else 
            throw new Error("just only can set type for an UNKNOWN_TYPE DrtVariable");
    }
    
    public String getName() {
        return this.name;
    }
    
    @Override
    public String toString() {
        return this.name;
    }

    @Override
    public boolean equals(Object other) {
        if (other instanceof DrtVariable) {
            DrtVariable that = (DrtVariable)other;
            return that.name.equals(this.name) && that.type.matches(this.type);
        }
        else 
            return false;
    }
    
    @Override
    public int hashCode() {
        return this.name.hashCode();
    }
    
    @Override
    public DrtExpression replace(DrtVariable var, DrtExpression exp) {
        if (this.equals(var))
            return exp;
        else 
            return this;
    }
    
    @Override
    public DrtVariable simplify() {
        return this;
    }

    @Override
    public void getRefs(LinkedHashSet<DRSReferent> refs) {
    }
    
    @Override
    public void getAllVariables(LinkedHashSet<DrtVariable> vars) {  
        vars.add(this);
    }

    @Override
    public Graph toGraph() {
        return new Graph();
    }
    
    @Override
    public DrtVariable getTemplate(Map<String,String> map) {
        return this;
    }
}

